package com.example.demo;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.regex.Matcher;
import java.util.regex.Pattern;


public class TestClass {
    public static Logger logger = LoggerFactory.getLogger(TestClass.class);

    public static void main(String[] args) {
//         System.out.println("xyh--"+ getDateByParse("2021-04-08 19:08:00"));
         System.out.println("xyh--"+getDateByParse("2021-04-08 19:28:00"));
         System.out.println("xyh--"+getDateByParse("2021-04-08 19:20:00"));
         System.out.println("xyh--"+getDateByParse("2021-04-08 18:30:00"));
         System.out.println("xyh--"+getDateByParse("2021-04-08 18:26:00"));
         System.out.println("xyh--"+getDateByParse("2021-04-08 16:20:00"));
         System.out.println("xyh--"+getDateByParse("2021-04-08 08:20:00"));
         System.out.println("xyh--"+getDateByParse("2021-04-07 23:20:00"));
         System.out.println("xyh--"+getDateByParse("2021-04-07 22:21:00"));
         System.out.println("xyh--"+getDateByParse("2021-04-07 16:20:00"));
        ArrayList<String> arrayList = new ArrayList<>();
        arrayList.add("#a1a1a1a1a");
        for (String a : arrayList) {
            System.out.println("结果：" + isOnlyLetterAndNum(a));
        }


    }

    public static String getDateByParse(String information_date) {
        String date = "";

        Date information_date_ = stringToDate(information_date, "yyyy-MM-dd HH:mm:ss");

        Calendar calendar = Calendar.getInstance();
        calendar.setTime(information_date_);

        Calendar calendar_ = Calendar.getInstance();

        if (calendar.get(Calendar.DAY_OF_MONTH) == calendar_.get(Calendar.DAY_OF_MONTH)) {
            long difference_date = System.currentTimeMillis() - information_date_.getTime();
            if (difference_date < 1000 * 60) {
                date = "刚刚";
            } else if (difference_date < 1000 * 60 * 60) {
                date = (difference_date / 1000 / 60) + "分钟前";
            } else if (difference_date < 1000 * 60 * 60 * 24) {
                date = (difference_date / 1000 / 60 / 60) + "小时前";
            } else {
                date = information_date.substring(0,10);
            }

        } else {
            date =  information_date.substring(0,10);
        }

        return date;
    }


    public static Date stringToDate(String dateString, String format) {
        SimpleDateFormat sdf = new SimpleDateFormat(format);
        try {
            return sdf.parse(dateString);
        } catch (ParseException e) {
            return new Date();
        }
    }


    public static boolean isOnlyLetterAndNum(String str) {
        final String REGEX = "^[a-z~]+$";
        final String REGEX1 = "^[0-9~]+$";
        final String REGEX2 = "^[a-z0-9~*]+$";
        Matcher m = Pattern.compile(REGEX, Pattern.CASE_INSENSITIVE).matcher(str);
        if (m.find()) {
            System.out.println("纯英");
            return false;
        } else {
            m = Pattern.compile(REGEX1, Pattern.CASE_INSENSITIVE).matcher(str);
            if (m.find()) {
                System.out.println("不能为纯数字");
                return false;
            } else {
                m = Pattern.compile(REGEX2, Pattern.CASE_INSENSITIVE).matcher(str);
                if (m.find()) {
                    System.out.println("英数混合");
                    return true;
                } else {
                    System.out.println("不能有特殊字符");
                    return false;
                }
            }
        }
    }

}